package org.neframework.webtest.action.dept;

import java.util.List;
import java.util.Map;

import org.neframework.webtest.model.t_fk_auth;
import org.neframework.webtest.model.t_fk_dept;
import org.neframework.webtest.service.DeptService;
import org.neframework.webtest.util.tree.TreeTools;
import org.neframework.webtest.base.BaseController;

import com.tmsps.neframework.core.utils.ChkTools;
import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/dept")
public class DeptController extends BaseController {

	private static final DeptService detpService = new DeptService();

	@RequestMapping("/tree")
	public String tree() {
		return "/jsp/dept/tree.jsp";
	}

	@RequestMapping("/dept_data")
	@ResponseBody
	public String loca_data() {

		List<Map<String, Object>> list = detpService.selectLocationList();

		return TreeTools.turnListToTree(list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_fk_dept dept, String up_code) {
		dept.setCode(up_code + dept.getCode());
		oc.saveObj(dept);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_fk_dept dept = (t_fk_dept) oc.findObjById(kid, t_fk_dept.class);
		return JsonTools.toJson("dept", dept);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_fk_auth auth) {
		t_fk_dept authDb = (t_fk_dept) oc.findObjById(auth.getKid(), t_fk_dept.class);
		authDb.setName(auth.getName());
		authDb.setCode(auth.getCode());
		authDb.setType(auth.getType());
		oc.updateObj(authDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_fk_dept.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

	@RequestMapping("/auth_unique")
	@ResponseBody
	public void auth_unique(String kid, String code) {
		t_fk_dept dept = null;

		if (ChkTools.isNull(kid)) {
			dept = detpService.findDeptByCode(code);
		} else {
			dept = detpService.findDeptByCode(kid, code);
		}

		if (dept == null) {
			result.put("success", "true");
		} else {
			result.put("fail", "true");
		}
	}
}
